﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemReceiptGroupForm : Form
    {
        private int ID = -1;
        private int isEdit = 1;
        public ThemReceiptGroupForm()
        {
            InitializeComponent();
        }
        public ThemReceiptGroupForm(int groupID, int edit)
        {
            InitializeComponent();
            isEdit = edit;
            ID = groupID;
            loadData();
        }
        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.ReceiptGroup ig = (from rg in dc.ReceiptGroups
                                where rg.ReceiptGroup_ID == ID
                                select rg).Single();

            txtName.Text = ig.ReceiptGroup_Name;
            txtNote.Text = ig.Note;
            txtStatus.Text = ig.ReceiptGroup_Status;
        }

      

      

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (isEdit == 0)
            {
                this.Close();
                return;
            }
            else
            {
                if (txtName.Text.Trim().Length == 0 || txtNote.Text.Trim().Length == 0 || txtStatus.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Cần điền đầy đủ thông tin");
                    return;

                }

                    _DB.ReceiptGroup newGroup = new _DB.ReceiptGroup();
                    newGroup.ReceiptGroup_Name = txtName.Text;
                    newGroup.Note = txtNote.Text;
                    newGroup.ReceiptGroup_Status = txtStatus.Text;
                    if (ID < 0)
                        _DB.ReceiptGroupDB.insert(newGroup);
                    else
                    {
                        newGroup.ReceiptGroup_ID = ID;
                        _DB.ReceiptGroupDB.update(newGroup);
                    }
                    this.Close();
                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (ID >= 0)
                loadData();
            else
            {
                txtName.Text = "";
                txtNote.Text = "";
                txtStatus.Text = "";
            }
        }
    }
}
